<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean"  prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"   prefix="c" %>
<%@page import="java.util.List"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/ui.jqgrid.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/redmond/jquery-ui-1.8.2.custom.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/operhc_style.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/buttonOHC.css" />
	
	<script type="text/javascript" src='<%= request.getContextPath()%>/dwr/interface/ProductoPlazoAction.js'></script>
	<script type='text/javascript' src='<%= request.getContextPath()%>/dwr/engine.js'></script>
	<script type='text/javascript' src='<%= request.getContextPath()%>/dwr/util.js'></script>
	<script src="<%=request.getContextPath()%>/js/util/gridUtil.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/jquery-1.7.1.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/i18n/grid.locale-es.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/jquery.jqGrid.src.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/grid.subgrid.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/util/encabezado.js" type="text/javascript"></script>
	
</head>

<script type="text/javascript" charset="UTF-8">

var arryPlazo = new Array();
var idTableProdPlazo = 'listProductoPlazo';

var lastSel = '';
var rutaContexto1 = location.pathname;
var rutaContexto2 = "<%=request.getContextPath()%>";
var rutaContexto  = rutaContexto1.substr(0, rutaContexto1.indexOf(rutaContexto2)) + rutaContexto2;

var myColNames  = ['', '','Plazo', 'Activo'];
var myDataModel = [ {name : 'codProducto',			index : 'codProducto',		width : VAL_WIDTH.SMALL, 	hidden:true, formatter: codProductoFormat, unformat: codProductoUnFormat},
                    {name : 'codPlazo',				index : 'codPlazo',			width : VAL_WIDTH.SMALL, 	hidden:true, formatter: codPlazoFormat, unformat: codPlazoUnFormat},
                    {name : 'desPlazo',				index : 'desPlazo' ,		width : VAL_WIDTH.MED 	 	},
                    {name : 'estSeleccion',			index : 'estSeleccion',		width : VAL_WIDTH.SMALL,	align:'center', formatter: checkProdPlazoFormat, unformat: checkProdPlazoUnFormat}];

if (jQuery.browser.msie){
	document.getElementsByName = function(name, tag){
	    return getElementsByName_iefix(name, tag);
	};
}

function codProductoFormat(cellvalue, options, rowObject){
	return cellvalue+"<input type='hidden' name='arrayCodProducto' value='"+cellvalue+"' />";
}

function codProductoUnFormat(cellvalue, options, cell){
	return $('input', cell).attr('value');
}

function codPlazoFormat(cellvalue, options, rowObject){
	return cellvalue+"<input type='hidden' name='arrayCodPlazo' value='"+cellvalue+"' />";
}

function codPlazoUnFormat(cellvalue, options, cell){
	return $('input', cell).attr('value');
}

function checkProdPlazoFormat(cellvalue, options, rowObject){
	var checkbox = '';
	if(cellvalue == '1' || cellvalue == 'Yes'){
		checkbox = "<input type='checkbox' onclick='selectCheckGrid(this, 2, arryPlazo, idTableProdPlazo);' name='arrayCheckProdPlazo' offval='no' value='"+cellvalue+"' checked='checked' />";
	}else{
		checkbox = "<input type='checkbox' onclick='selectCheckGrid(this, 2, arryPlazo, idTableProdPlazo);' name='arrayCheckProdPlazo' offval='no' value='"+cellvalue+"' />";
	}
	
	return checkbox;
}

function checkProdPlazoUnFormat(cellvalue, options, cell){
	var checked = $('input', cell).attr('checked');
	if(checked == undefined){
		return "0";
	}else{
		return "1";
	}
}

function viewAdminMain(){
	
	var formulario = document.getElementById('formProdPlazo');
	formulario.action = rutaContexto+'/adminForm.do?method=verListado';
	formulario.submit();
}

function consultarProductoPlazos(operation, message){

	var formulario = document.getElementById('formProdPlazo');
	var codProductoPadre = formulario.codProductoPadre.value;
	
	jQuery("#listProductoPlazo").GridUnload();
	ProductoPlazoAction.consultarProductoPlazosAjax(codProductoPadre, function(data){
		
		mostrarTabla(data);
		if(operation == 'logicDelete'){
			alert(message);
		}
	});
}

function saveConfigurationPlazoForm(){
	
	var formulario = document.getElementById('formProdPlazo');
	var isOk = true;
	
	if(isOk){		
		var selPlazo = searchCheckGrid(arryPlazo);
		formulario.selPlazo.value = selPlazo;
	}
	
	if(isOk){
		showLoading();
		formulario.action = rutaContexto+'/productoPlazoForm.do?method=saveConfigurationForm';
		formulario.submit();
	}
}

function mostrarTabla(data){
	
	var idTableForm = 'listProductoPlazo';
	$('body').append('<div id="paginador_listProductoPlazo" class="grid"></div>'); 
	var paginador = "paginador_listProductoPlazo";
	
	jQuery("#"+idTableForm).jqGrid(
	{
		beforeSelectRow: function(){},
	    caption		: "Listado de Plazos - Producto",
		data 	 	: data,
		datatype 	: "local",
		height   	: "100%",
		weight 	 	: 1000,
		colNames 	: myColNames,
		colModel 	: myDataModel,
		rowList 	: [5,10,15,20,25],
		rowNum 		: 15, 
		pager 		: paginador,
		viewrecords : true,
		loadonce	: true,
		multiselect : true,			
		subGrid    	: false,
		footerrow  	: false,
		loadComplete :
           function (data) {
			initialCheckGrid(arryPlazo, idTableProdPlazo, 2, 2, 'arrayCheckProdPlazo');
			recordCheckGrid(idTableProdPlazo, arryPlazo, 'codPlazo', 'arrayCheckProdPlazo');
		}
	});
}

</script>

<body onload="closeLoading();consultarProductoPlazos('', '');scrollTopForm();">
<html:form styleId="formProdPlazo" method="post">
<html:hidden property="codProductoPadre" value="${command.codProductoPadre}" styleId="codProductoPadre"/>
<html:hidden property="desProductoPadre" value="${command.desProductoPadre}" styleId="desProductoPadre"/>
<html:hidden property="selPlazo" styleId="selPlazo"/>

	<div style="background-color: #0066bb;">
		<font face="Arial Narrow" size=3 color="#FFFFFF"><b>&nbsp;
		<bean:message key="etiqueta.titleAdminSist"/>
		</b></font>
	</div>
	
	<center>
	<%
	Object messageError = request.getAttribute("messageError");
	if(messageError != null){
		if(messageError instanceof List){
			
			List listMsg = (List)messageError;
			if(!listMsg.isEmpty()){%>
				
				<div class="error">
					<% 
					for(int i = 0; i < listMsg.size(); i++){%>
						<img alt="" src="images/icono_error.png" width="17px" height="17px" class="icon">
			    		<%=listMsg.get(i)%><br/>
					<%}
					%>	
				</div>
			<%}			
			
		}else{
			if(!"".equals(messageError)){%>
				
				<div class="error">
			    	<img alt="" src="images/icono_error.png" width="17px" height="17px" class="icon">
			    	<%=messageError%><br/>
			    </div>				
			<%}
		}
	}
	%>
	

	<%
	Object messageInfo = request.getAttribute("messageInfo");
	if(messageInfo != null){
		if(messageInfo instanceof List){
			
			List listMsg = (List)messageInfo;
			if(!listMsg.isEmpty()){%>
				
				<div class="message">
					<% 
					for(int i = 0; i < listMsg.size(); i++){%>
						<img alt="" src="images/information-icon.png" width="17px" height="17px" class="icon">
			    		<%=listMsg.get(i)%><br/>
					<%}
					%>	
				</div>
			<%}			
			
		}else{
			if(!"".equals(messageInfo)){%>
				
				<div class="message">
			    	<img alt="" src="images/information-icon.png" width="17px" height="17px" class="icon">
			    	<%=messageInfo%><br/>
			    </div>				
			<%}
		}
	}
	%>
	</center>
	
	<br/>
		<%--
		<a href="javascript:viewAdminMain();"><img src="imagenes/btregresar-1.jpg" border="0"></a>
		 --%>
		 
		<a href="javascript:viewAdminMain();" class="buttonOHC">REGRESAR</a>
		<br/><br/>
	
		<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
		<td>
			<font face="Arial Narrow" size=3 color="#000080"><b>Configuración de Plazos</b><BR>
			Producto <c:out value="${command.codProductoPadre}"/>: <c:out value="${command.desProductoPadre}"/>
			</font><br><br>
			
			<a href="javascript:saveConfigurationPlazoForm();" class="buttonOHC">GUARDAR</a>
			<%--
			<a href="javascript:saveConfigurationPlazoForm();"><img src="imagenes/btguardar-1.jpg" border="0"></a>
			--%>
			<br/><br/>
			
		</td>
		</tr>
		</table>
		
		<table id="listProductoPlazo" class="grid">
		</table>
		
</html:form>
</body>
</html>